<template>
  <div class="edit-container">
    <!-- 导航条 -->
    <van-nav-bar left-arrow @click-left="left" title="编辑资料"></van-nav-bar>
    <!-- 头像部分 -->
    <div class="avatar">
      <van-image fit="cover" round :src="userInfo.photo" />
      <!-- 上传组件 -->
      <van-uploader :after-read="afterRead" />
    </div>
    <!-- 信息展示 -->
    <van-cell-group>
      <van-cell
        is-link
        title="名称"
        @click="showNameDialog"
        :value="userInfo.name"
      />
      <van-cell
        is-link
        title="性别"
        :value="userInfo.gender == 0 ? '男' : '女'"
        @click="showGender = true"
      />
      <van-cell
        is-link
        title="生日"
        @click="showBirthdayPop"
        :value="userInfo.birthday"
      />
    </van-cell-group>
    <!-- 姓名编辑框 -->
    <van-dialog
      v-model="showName"
      @confirm="saveName"
      title="修改昵称"
      show-cancel-button
    >
      <van-field ref="nameField" type="text" v-model="name" />
    </van-dialog>
    <!-- 性别编辑框 -->
    <van-popup v-model="showGender" position="bottom">
      <!-- 顶部导航栏 -->
      <van-nav-bar
        title="修改性别"
        left-text="取消"
        @click-left="showGender = false"
      />
      <!-- 性别选择区域 -->
      <van-cell-group>
        <van-cell title="男" is-link @click="changeGender(0)" />
        <van-cell title="女" is-link @click="changeGender(1)" />
      </van-cell-group>
    </van-popup>
    <!-- 生日选择区域 -->
    <van-popup position="bottom" v-model="showBirthday">
      <van-datetime-picker
        v-model="birthday"
        @cancel="showBirthday = false"
        @confirm="confirmData"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
      />
    </van-popup>
    <!-- 裁切区域弹框 -->
    <div class="cropper-box" v-show="isShowCropper">
      <!-- 裁切组件 -->
      <vueCropper
        ref="cropper"
        fixed
        autoCropWidth="120"
        autoCropHeight="120"
        autoCrop
        :img="img"
      ></vueCropper>
      <!-- 2个按钮 -->
      <van-button class="confirm-btn" @click="comfirmUpload" type="primary"
        >确认</van-button
      >
      <van-button class="cancel-btn" @click="isShowCropper = false" type="info"
        >取消</van-button
      >
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import moment from 'moment'
import { VueCropper } from 'vue-cropper'
import { editUserInfo, uploadPhoto } from '../../api/edit'

export default {
  name: 'edit',
  data () {
    return {
      showName: false,
      name: '',
      showGender: false,
      showBirthday: false,
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(2025, 10, 1),
      birthday: new Date(1998, 0, 17),
      img: '',
      isShowCropper: false
    }
  },
  components: {
    VueCropper // 注册切图组件
  },
  methods: {
    // 点击确认上传头像
    comfirmUpload () {
      // 通过截图组件获取 文件的 blob信息
      this.$refs.cropper.getCropBlob(data => {
        // 组成formdata
        const formData = new FormData()
        formData.append('photo', data)

        uploadPhoto(formData).then(res => {
          this.$store.commit('setUserInfo', {
            ...this.userInfo,
            photo: res.data.data.photo
          })
          this.isShowCropper = false
        })
      })
    },
    left () {
      this.$router.push({ path: '/my' })
    },
    showNameDialog () {
      this.showName = true
      this.name = this.userInfo.name

      // dom更新完再去获取焦点
      this.$nextTick(() => {
        this.$refs.nameField.focus()
      })
    },
    // 修改名字
    saveName () {
      // console.log('aaa')
      editUserInfo({ name: this.name }).then(res => {
        console.log('aaa')
        this.$toast.success('修改成功')
        // 更新本地数据
        this.$store.commit('setUserInfo', {
          ...this.userInfo,
          // 使用最新数据覆盖原始数据
          name: res.data.data.name
        })
      })
    },
    // 修改性别
    changeGender (gender) {
      console.log(gender)
      editUserInfo({ gender }).then(res => {
        this.$store.commit('setUserInfo', {
          ...this.userInfo,

          gender: res.data.data.gender
        })
        this.$toast.success('修改成功')
      })

      this.showGender = false
    },
    // 弹出生日编辑框
    showBirthdayPop () {
      this.showBirthday = true
      // 设置用户的生日为默认值
      this.birthday = new Date(this.userInfo.birthday)
    },
    confirmData () {
      console.log('aaa')
      const birthday = moment(this.birthday).format('YYYY-MM-DD')

      editUserInfo({ birthday }).then(res => {
        this.$toast.success('修改成功')
        this.showBirthday = false
        this.$store.commit('setUserInfo', {
          ...this.userInfo,
          birthday: res.data.data.birthday
        })
      })
    },
    // 选择文件之后执行的回调函数
    afterRead (file) {
      // 此时可以自行将文件上传至服务器
      console.log(file)
      // 选择文件后显示裁切窗口
      this.isShowCropper = true
      this.img = file.content

      // 实例化formData
      // const data = new FormData()
      // data.append('photo', file.file)
      // uploadPhoto(data).then(res => {
      //   // console.log('res:', res)
      //   this.$toast.success('更新成功')
      //   this.$store.commit('setUserInfo', {
      //     ...this.userInfo,
      //     photo: res.data.data.photo
      //   })
      // })
    }
  },
  computed: mapState(['userInfo'])
}
</script>

<style lang="less">
.edit-container {
  // 导航条部分
  // .van-nav-bar {
  //   background-color: #3196fa;
  //   .van-nav-bar__title {
  //     color: #fff;
  //   }
  //   .van-icon-arrow-left {
  //     color: #fff;
  //   }
  // }
  // 头像部分
  .avatar {
    padding: 20px 0;
    text-align: center;
    .van-image {
      width: 120px;
      height: 120px;
    }
  }

  .van-uploader {
    // 上传组件
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    height: 120px;
    width: 120px;
    opacity: 0;
    .van-uploader__upload {
      width: 120px;
      height: 120px;
    }
  }

  // 裁切弹框样式
  .cropper-box {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    .confirm-btn {
      position: absolute;
      right: 0;
      bottom: 0;
    }
    .cancel-btn {
      position: absolute;
      left: 0;
      bottom: 0;
    }
  }
}
</style>
